// src/router/index.ts
import { createRouter, createWebHistory } from 'vue-router'

import ecoCoral from '../components/EcoCoral.vue'
import heatwave from '../views/heatwave.vue'
import heatStrength from '../views/heatStrength.vue'

const routes = [
      {
    path: '/',
    redirect: '/parent'  // 访问根路径时自动跳转
  },
  {
    path: '/parent',
    component: ecoCoral,
    children: [
      {
        path: 'heatwave',
        component: heatwave,
      },
      {
        path: 'heatStrength',
        component: heatStrength,
      },
    ],
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})
let firstNavigation = true

router.beforeEach((to, from, next) => {
  if (firstNavigation) {
    firstNavigation = false

    const navEntry = performance.getEntriesByType("navigation")[0] as PerformanceNavigationTiming
    const isReload = navEntry.type === "reload"

    if (isReload && to.path.startsWith("/parent/") && to.path !== "/parent") {
      return next("/parent")
    }
  }

  next()
})
export default router
